/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *  * Jason this is the latest code V1.0
 */

package edu.ipfw.cs590.cheat.services.utils.messages;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import javax.activation.DataHandler;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPMessage;

/**
 *
 * @author rrm46582
 */
public class ManagementMessage extends CheatMessage
{
    static final private String                 SERVICE_URL = "http://robinrmcomputer:8080/CheatApp-war/AssignmentManagementSAAJ";
    static final public String                  SUBMIT_SERVICE  = "SubmitAssignment";
    static final public String                  REPLACE_SERVICE = "ReplaceAssignment";
    static final public String                  UUID_PARAMETER  =  "UUID";


    private String managementMethod;
    private HashMap<String, String> parameterList;
    private AttachmentPart targetAttachment;
    private byte[] attachmentBytes;



    public ManagementMessage(String method, String token)
    {
        super(token, method);
        parameterList = new HashMap<String,String>();
        managementMethod = method;
    }

    @Override
    public String getServiceName()
    {
        return managementMethod;
    }

    @Override
    protected String getURL()
    {
        return SERVICE_URL;
    }

    @Override
    public HashMap<String, String> params()
    {
        return parameterList;
    }


    public String call()
    {
        String returnValue;

        try
        {
            
            addParams();
            // Get a SOAPConnection object and call the service
    		SOAPConnectionFactory soapConnectionFactory =  SOAPConnectionFactory.newInstance();
        	SOAPConnection connection = soapConnectionFactory.createConnection();
        	SOAPMessage response = connection.call(message, SERVICE_URL);

            // Check for exceptions
           if ( response.getSOAPBody().getFault() != null )
           {
               System.out.println(response.getSOAPBody().getFault().toString());
               throw new RuntimeException(response.getSOAPBody().getFault().toString());
            }

            // Process the response message
            SOAPBody soapBody = response.getSOAPBody();
            returnValue = soapBody.getElementsByTagName("result").item(0).getTextContent();
            connection.close();
            return returnValue;
        }
        catch(Exception e)
        {
            throw new RuntimeException(e.getMessage());
        }
    }



    public void setZip(byte[] zip)
    {
		try
        {
        if ( targetAttachment != null )
            throw new RuntimeException("Attachment Already Set");

        String encodedZip = Base64.encode(zip);
        DataHandler h = new DataHandler(encodedZip, "text/plain");
        targetAttachment = message.createAttachmentPart(h);
        targetAttachment.setContentId("assignment");
        message.addAttachmentPart(targetAttachment);
		}
        catch (Exception e)
        {
			e.printStackTrace();
		}
	}

    public byte[] getZip()
    {
        return attachmentBytes;
    }


    public boolean isZipSet()
    {
        return targetAttachment == null;
    }

    public void clearZip()
    {
        targetAttachment = null;
    }
}

